const Minio = require('minio');
const endPoint = think.config('minio.endPoint');
const port = think.config('minio.port');
const useSSL = think.config('minio.useSSL');
const accessKey = think.config('minio.accessKey');
const secretKey = think.config('minio.secretKey');

const minioClient = new Minio.Client({
  endPoint: endPoint,
  port: port,
  useSSL: useSSL,
  accessKey: accessKey,
  secretKey: secretKey
});

module.exports = class extends think.Service {
  /**
     * 上传文件
     */
  async uploadFile(bucketName, filePath, fileName) {
    console.log('文件路径', filePath);

    const metaData = {
      'Content-Type': 'application/octet-stream'
    };

    try {
      await minioClient.fPutObject(bucketName, fileName, filePath, metaData);
      return fileName;
    } catch (e) {
      return 'error';
    }
  }

  /**
     * 删除文件
     */
  async deleteFile(bucketName, fileName) {
    try {
      const result = await minioClient.removeObject(bucketName, fileName);
      return this.success(result);
    } catch (e) {
      return 'error';
    }
  }
};
